package ru.amse.stroganova.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * This class provides a dialog for choosing new graph type.
 * 
 * @author Dasha Stroganova
 *
 */
public class NewGraphDialog extends JDialog {

	private boolean isDirected;
	
	private boolean isWeighted;
	
	private boolean wasTypeChoosen;
	
	/**
	 * Creates new graph type chooser dialog.
	 * 
	 * @param frame
	 */
	public NewGraphDialog(Frame frame) {
		super(frame, "New graph", true);
		createRootPane();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setFocusable(true);
		
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
		
		JLabel label = new JLabel("Choose graph type:");
		label.setAlignmentX(Component.CENTER_ALIGNMENT);
		getContentPane().add(label);
		getContentPane().add(createDirectedPanel());
		getContentPane().add(createWeightPanel());
		getContentPane().add(createOKCancelPanel());
		
		addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					wasTypeChoosen = true;
				} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					wasTypeChoosen = false;
				}
				dispose();
			}
		});
	
		//TODO place it in a better way
		setLocationRelativeTo(frame);
		pack();
		setResizable(false);
		requestFocusInWindow();
		setVisible(true);
	}
	
	private JPanel createDirectedPanel() {
		JPanel directPanel = new JPanel();
		directPanel.setLayout(new BoxLayout(directPanel, BoxLayout.PAGE_AXIS));
		directPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		directPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		ButtonGroup dirGroup = new ButtonGroup();
		
		JRadioButton directed = new JRadioButton("directed");
		dirGroup.add(directed);
		directPanel.add(directed);
		directed.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isDirected = true;
				NewGraphDialog.this.requestFocusInWindow();
			}
		});
		directed.setSelected(true);
		isDirected = true;
		
		JRadioButton undirected = new JRadioButton("undirected");
		dirGroup.add(undirected);
		directPanel.add(undirected);
		undirected.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isDirected = false;
				NewGraphDialog.this.requestFocusInWindow();
			}
		});
		
		return directPanel;
	}
	
	private JPanel createWeightPanel() {
		JPanel weightPanel = new JPanel();
		weightPanel.setLayout(new BoxLayout(weightPanel, BoxLayout.PAGE_AXIS));
		weightPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		weightPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		ButtonGroup weightGroup = new ButtonGroup();
		
		JRadioButton weighted = new JRadioButton("weighted");
		weightGroup.add(weighted);
		weightPanel.add(weighted);
		weighted.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isWeighted = true;
				NewGraphDialog.this.requestFocusInWindow();
			}
		});
		isWeighted = true;
		weighted.setSelected(true);
		
		JRadioButton unweighted = new JRadioButton("unweighted");
		weightGroup.add(unweighted);
		weightPanel.add(unweighted);
		unweighted.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isWeighted = false;
				NewGraphDialog.this.requestFocusInWindow();
			}
		});
		
		return weightPanel;
	}
	
	private JPanel createOKCancelPanel() {
		JPanel okCancelPanel = new JPanel();
		
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				wasTypeChoosen = true;
				dispose();
			}
		});
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				wasTypeChoosen = false;
				dispose();
			}
		});
		
		okCancelPanel.add(okButton);
		okCancelPanel.add(cancelButton);
		return okCancelPanel;
	}
	
	/**
	 * Veryfies whether used did choose graph type.
	 * 
	 * @return true if he has; false otherwise
	 */ 
	//TODO think of a better way to retrieve this info
	public boolean wasTypeChoosen() {
		return wasTypeChoosen;
	}
	
	/**
	 * Returns whether user has choosen directed or undirected graph. If user didn't choose IllegalStateException is thrown. 
	 * 
	 * @return true if directed; false otherwise.
	 */
	public boolean isDirected() {
		if (!wasTypeChoosen) {
			throw new IllegalStateException("User didn't choose anything");
		}
		return isDirected;
	}
	
	/**
	 * Returns whether user has choosen directed or undirected graph. If user didn't choose IllegalStateException is thrown.
	 * 
	 * @return true if directed; false otherwise.
	 */
	public boolean isWeighted() {
		if (!wasTypeChoosen) {
			throw new IllegalStateException("User didn't choose anything");
		}
		return isWeighted;
	}
}
